<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>


        const obj = {
            b: 2
        }



        Object.defineProperty(obj, "a", {
            get() {
                console.log("运行了属性a的get函数")
                return obj._a;
            },
            set(val){
                console.log("运行了属性a的set函数", val)
                obj._a = val;
            }
        })


        obj.a = 20 + 10; // 自动运行 set(20 + 10)
        console.log(obj.a); // console.log(get())

        obj.a = obj.a + 1; // set(obj.a + 1)   set(get() + 1)
        console.log(obj.a);


        obj.a = 10;

        console.log(obj);


    </script>
</body>

</html>